<?php

namespace Admin\Controller;
/**救护控制器
 * Class UserController
 * @package Admin\Controller
 */
class RescueController extends AdminController {

   
    /**
     * 救护列表
     * @author leiying
     */
    public function rescueList(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_rescue',$get);
        }else{
            if(I('get.p')){
                $this->assign('p',I('get.p'));
            }else{
                session('searchdata_rescue',null);
            }
            $get = $search_data = session('searchdata_rescue');
        }
        if(isset($get['status']) && $get['status'] != ''){
            $search_data['status'] = $get['status'];
            $where['a.status'] = array('eq',$get['status']);
        }
 
        $get['sort'] = isset($get['sort'])?$get['sort']:'desc';
        $search_data["sort"] = $get["sort"];
        

        $count = M('rescue_log')->where($where)->count();
        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数

        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出 
        $lists = M('rescue_log')->alias('a')->field('a.*,b.user_name,c.user_name as rescue_name,d.name as type_name')
            ->join('fas_users b on a.user_id = b.user_id ','left')
            ->join('fas_users c on a.rescue_id = c.user_id','left')
            ->join('fas_rescue_type d on a.rescue_type = d.id','left')
            ->where($where)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order("a.id ".$get['sort'])
            ->select();
      
        $this->assign('lists',$lists);
        $p = I('get.p',1);
        $this->assign('p',$p);
        $this->assign('searchs',$search_data);
        $this->display();
    }

    //救护详情
    public function rescueInfo(){
        if(I('post.btn')){$this->success('');}
        $info= M('rescue_log')->alias('a')->field('a.*,b.user_name,c.user_name as rescue_name,d.name as type_name')
            ->join('fas_users b on a.user_id = b.user_id ','left')
            ->join('fas_users c on a.rescue_id = c.user_id','left')
            ->join('fas_rescue_type d on a.rescue_type = d.id','left')
            ->where('a.id='.I('id'))
            ->find();
        // echo M()->getLastsql();
        // exit;
        $this->assign('res',$info);
        $this->display();
    }

    public function commentCheck(){  
        $data=I('post.');
        $id=$data['id'];
        $info['is_check']=isset($data['status'])?$data['status']:1;
        $res=M('comment')->where('id='.$id)->save($info);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    public function del(){
        $id=$_POST['id'];
        $res=M('rescue_log')->where('id='.$id)->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    //评价
    public function rescueComment(){
        if(I('post.btn')){$this->success('');}
        //$info=M('comment')->where('id='.I('comment_id'))->find();

        $info= M('comment')->alias('a')->field('a.*,b.user_name')
            ->join('fas_users b on a.uid = b.user_id ','left')
            ->where('a.id='.I('comment_id'))
            ->find();

        $this->assign('res',$info);
        $this->display();
    }


    //救护类型
    public function categoryList(){
        $lists=$tree_list = M('rescue_type')->order("id asc")->select();
        $ztree = json_encode($lists);
        $this->assign('ztree',$ztree);
        $this->display();
    }


    public function categoryAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $data = I('post.');
            $path = "/Uploads/Rescue/" . date("Y-m-d") . "/";
            if($data['cover_pic']){
                $url = $this->upload_pic($data['cover_pic'],$path);
                if($url !== false){$data['logo'] = $url;}else{$this->error('图片上传失败');}
            }
            //验证通过了
            $res = M('rescue_type')->add($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
          $this->display();  
        }
        
    }
     public function categoryEdit(){
        if(IS_POST){
            $data = I('post.');
            $id = $data['id'];
            //处理参数
            if(empty($id)){
              $this->error('请选择需要操作的数据');
            }
            
            $row = M('rescue_type')->where(array('id'=>$id))->find();
            $path = "/Uploads/Rescue/". date("Y-m-d") . "/";
            if($data['new_cover_pic']){
                $url = $this->upload_pic($data['new_cover_pic'],$path);
                if($url !== false){
                    $delete_path = $_SERVER['DOCUMENT_ROOT'].$row['logo'];
                    @unlink($delete_path);
                    $data['logo'] = $url;
                }else{$this->error('图片上传失败');}
            }
            $res = M('rescue_type')->where(array('id'=>$id))->save($data);
            if(is_numeric($res)){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{

          $data=M('rescue_type')->where('id='.I('id'))->find();
          $this->assign('data',$data);
          $this->display();  
        }
        
    }

    //删除类型
    public function delRescueType(){
        $id=I('post.id',0,'intval');
        $res = M('rescue_type')->where(array('id'=>$id))->delete();
        if($res){
           $this->success('删除成功');
        }else{
           $this->error('删除失败');
        }
    }

        //救护类型
    public function reasonList(){
        $lists=$tree_list = M('reason')->order("id asc")->select();
        $ztree = json_encode($lists);
        $this->assign('ztree',$ztree);
        $this->display();
    }


    public function reasonAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $data = I('post.');
            //验证通过了
            $res = M('reason')->add($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
          $this->display();  
        }
        
    }
     public function reasonEdit(){
        if(IS_POST){
            $data = I('post.');
            $id = $data['id'];
            //处理参数
            if(empty($id)){
              $this->error('请选择需要操作的数据');
            }
            $res = M('reason')->where(array('id'=>$id))->save($data);
            if(is_numeric($res)){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{

          $data=M('reason')->where('id='.I('id'))->find();
          $this->assign('data',$data);
          $this->display();  
        }
        
    }

    //删除类型
    public function delReason(){
        $id=I('post.id',0,'intval');
        $res = M('reason')->where(array('id'=>$id))->delete();
        if($res){
           $this->success('删除成功');
        }else{
           $this->error('删除失败');
        }
    }





}
